package com;

import java.io.IOException;
import java.io.PrintWriter;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import connection.DBConnection;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class QuickServlet
 */
@WebServlet("/QuickServlet")
public class QuickServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public QuickServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out = response.getWriter();
		String languages = "",mst="";
		String lang[]=request.getParameterValues("voucher");
		String ms[]=request.getParameterValues("master");
		String updatecust="",updatemaster="";

int j=0,k=0,l=0;
		DBConnection dbc=new DBConnection();	
		 Connection con=dbc.getNewConnection();
		 
		 Statement st = null;
		 ResultSet rs = null;
		 
		  if (request.getParameter("first") != null) {
		 try 
			{
				st = con.createStatement();
				
				for(int i=0;i<lang.length;i++){
				    languages=lang[i];
				     updatecust = "insert into vouchers(voucher) values('"+languages+"');";
				     j=st.executeUpdate(updatecust);
				}
				
				
		
				if(j>0){
					RequestDispatcher requestDispatcher; 
			        requestDispatcher = request.getRequestDispatcher("QuickFunction.jsp" ) ;
			        requestDispatcher.forward(request,response);
			        }
					else
		               out.print("Sorry..!! Got an exception.");
				
			}
			catch (SQLException e) 
			{
				e.printStackTrace();
			}
		  }
		  
		  else if (request.getParameter("second") != null) {
			  try 
				{
					st = con.createStatement();
					
					for(int i=0;i<lang.length;i++){
					    languages=lang[i];
					    System.out.println(languages);
					     updatecust = "Delete from vouchers where voucher='"+languages+"';";
					     j=st.executeUpdate(updatecust);
					}
					
					
			
					if(j>0){
						RequestDispatcher requestDispatcher; 
				        requestDispatcher = request.getRequestDispatcher("QuickFunction.jsp" ) ;
				        requestDispatcher.forward(request,response);
				        
				        }
						else
			               out.print("Sorry..!! Got an exception.");
					
				}
				catch (SQLException e) 
				{
					e.printStackTrace();
				}
	}}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
